package com.feeyo.groundservice.base.rabbitmq;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feeyo.groundservice.base.util.PropertiesUtils;
import com.rabbitmq.client.*;

public class RmqSync {
	Logger logger = LoggerFactory.getLogger(RmqSync.class);
	
	ConnectionFactory factory = null;
    Connection connection = null;
    
    

    public RmqSync() {
    	factory = new ConnectionFactory();
        factory.setHost(PropertiesUtils.getProperty("rabbitmq.host"));
        factory.setPort(Integer.parseInt(PropertiesUtils.getProperty("rabbitmq.port")));
        factory.setUsername(PropertiesUtils.getProperty("rabbitmq.user"));
        factory.setPassword(PropertiesUtils.getProperty("rabbitmq.password"));
        factory.setVirtualHost("/");
        factory.setAutomaticRecoveryEnabled(true);
        factory.setConnectionTimeout(5000);
        factory.setNetworkRecoveryInterval(5000l);
        factory.setRequestedHeartbeat(30);

        try {
            connection = factory.newConnection();
            connection.addShutdownListener(new ShutdownListener() {
                @Override
                public void shutdownCompleted(ShutdownSignalException cause)
                {

                    String hardError = "";
                    String applInit = "";

                    if (cause.isHardError()) {
                        hardError = "connection";
                    } else {
                        hardError = "channel";
                    }

                    if (cause.isInitiatedByApplication()) {
                        applInit = "application";
                    } else {
                        applInit = "broker";
                    }

                    logger.info("RMQ Connectivity to MQ has failed.  It was caused by "
                            + applInit + " at the " + hardError
                            + " level.  Reason received " + cause.getReason());
                }
            });

            ((Recoverable)connection).addRecoveryListener(new RecoveryListener() {
                @Override
                public void handleRecovery(Recoverable recoverable) {
                    if( recoverable instanceof Connection ) {
                        logger.info("RMQ Connection was recovered.");
                    } else if ( recoverable instanceof Channel ) {
                        int channelNumber = ((Channel) recoverable).getChannelNumber();
                        logger.info("RMQ Connection to channel #"
                                + channelNumber + " was recovered." );
                    }
                }

                @Override
                public void handleRecoveryStarted(Recoverable recoverable) {
                    logger.info("RMQ recovered started");
                }
            });

            logger.info("Rabbitmq telegram-> conneted");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
	}

	public void init() {
        
    }

    public Connection getConnection(){
        return connection;
    }

}
